from datetime import datetime
from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_sale.entity.vo.packaging_vo import PackagingPageQueryModel
from module_sale.service.price_service import PriceService
from module_sale.entity.vo.price_vo import DeletePriceModel, PriceModel, PricePageQueryModel, CalculatePriceQueryModel, \
    SalePriceDetailModel
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


salePriceController = APIRouter(prefix='/sale/price', dependencies=[Depends(LoginService.get_current_user)])


@salePriceController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('purchase:price:list'))]
)
async def get_sale_price_list(
        request: Request,
        price_page_query: PricePageQueryModel = Depends(PricePageQueryModel.as_query),
        query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    price_page_query_result = await PriceService.get_price_list_services(query_db, price_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=price_page_query_result)


@salePriceController.post('', dependencies=[Depends(CheckUserInterfaceAuth('purchase:price:add'))])
@ValidateFields(validate_model='add_price')
@Log(title='每日销售价记录', business_type=BusinessType.INSERT)
async def add_purchase_price(
    request: Request,
    add_price: PriceModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user)
):
    add_price.fabur = current_user.user.user_name
    add_price.fabusj = datetime.now()


    add_price_result = await PriceService.add_price_services(query_db, add_price)
    logger.info(add_price_result.message)

    return ResponseUtil.success(msg=add_price_result.message)


@salePriceController.put('', dependencies=[Depends(CheckUserInterfaceAuth('purchase:price:edit'))])
@ValidateFields(validate_model='edit_price')
@Log(title='每日销售价记录', business_type=BusinessType.UPDATE)
async def edit_purchase_price(
    request: Request,
    edit_price: PriceModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_price.update_by = current_user.user.user_name
    edit_price.update_time = datetime.now()
    edit_price_result = await PriceService.edit_price_services(query_db, edit_price)
    logger.info(edit_price_result.message)

    return ResponseUtil.success(msg=edit_price_result.message)


@salePriceController.delete('/{record_ids}', dependencies=[Depends(CheckUserInterfaceAuth('purchase:price:remove'))])
@Log(title='每日销售价记录', business_type=BusinessType.DELETE)
async def delete_purchase_price(request: Request, record_ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_price = DeletePriceModel(record_ids = record_ids)
    delete_price_result = await PriceService.delete_price_services(query_db, delete_price)
    logger.info(delete_price_result.message)

    return ResponseUtil.success(msg=delete_price_result.message)


@salePriceController.get(
    '/{record_id}', response_model=PriceModel, dependencies=[Depends(CheckUserInterfaceAuth('purchase:price:query'))]
)
async def query_detail_purchase_price(request: Request, record_id: int, query_db: AsyncSession = Depends(get_db)):
    price_detail_result = await PriceService.price_detail_services(query_db, record_id)
    logger.info(f'获取record_id为{record_id}的信息成功')

    return ResponseUtil.success(data=price_detail_result)


@salePriceController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('purchase:price:export'))])
@Log(title='每日销售价记录', business_type=BusinessType.EXPORT)
async def export_purchase_price_list(
    request: Request,
    price_page_query: PricePageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    price_query_result = await PriceService.get_price_list_services(query_db, price_page_query, is_page=False)
    price_export_result = await PriceService.export_price_list_services(price_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(price_export_result))


@salePriceController.get('/calculator/list', response_model=SalePriceDetailModel)
async def calculator_sale_price(
        request: Request,
        page_query: CalculatePriceQueryModel = Depends(CalculatePriceQueryModel.as_query),
        query_db: AsyncSession = Depends(get_db),
):
    # 获取不分页数据
    calculate_price_page_query_result = await PriceService.get_calculate_price_services(
        query_db, page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=calculate_price_page_query_result)